#include "Sprite.h"
#include "DirectX.h"
#include <string>
using namespace std;

Sprite::Sprite()
{
	//Initialize the current textures
	D3DXMatrixIdentity(&spriteMat);
	D3DXMatrixIdentity(&spriteScale);
	D3DXMatrixIdentity(&spriteRotate);
	D3DXMatrixIdentity(&spriteTranslate);
}

void Sprite::Init()
{
	//Create a sprite object using the directX function (this only needs to be done once)
	D3DXCreateSprite(DirectXFramework::Instance()->getDevice(), &sprite);
}

void Sprite::Draw(SpriteInfo & aSpriteInfo)
{
	D3DXMatrixIdentity(&spriteMat);
	D3DXMatrixIdentity(&spriteScale);
	D3DXMatrixIdentity(&spriteRotate);
	D3DXMatrixIdentity(&spriteTranslate);

	//Begin drawing the sprite
	sprite->Begin(D3DXSPRITE_ALPHABLEND | D3DXSPRITE_SORT_DEPTH_FRONTTOBACK);


	D3DXMatrixTranslation(&spriteTranslate, (float)aSpriteInfo.mPosOfCenter.X, (float)aSpriteInfo.mPosOfCenter.Y, 0.0f);
	D3DXMatrixRotationZ(&spriteRotate, D3DXToRadian(aSpriteInfo.mOrientation));
	D3DXMatrixScaling(&spriteScale, aSpriteInfo.mScale, aSpriteInfo.mScaleY, aSpriteInfo.mScale);

	D3DXMatrixMultiply(&spriteScale, &spriteScale, &spriteRotate);		//Multiply scale and rotation, stored in scale
	D3DXMatrixMultiply(&spriteMat, &spriteScale, &spriteTranslate);		//Multiply scale and translation, stored in world
	
	//Set the transform
	sprite->SetTransform(&spriteMat);

	//Draw the sprite object with the texture
	sprite->Draw(aSpriteInfo.mpTexture,
		0, &D3DXVECTOR3( (float)aSpriteInfo.mCenter.X, (float)aSpriteInfo.mCenter.Y, 0.0f),
		0, aSpriteInfo.mColor);
	//End drawing the sprite
	sprite->End();
}

void Sprite::Release()
{
	sprite->Release();
}